//
//  ZFBMomentCell.m
//  生活圈
//
//  Created by heima on 16/6/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "Masonry.h"
#import "UIColor+Addition.h"
#import "UILabel+Addition.h"
#import "ZFBMomentCell.h"

@interface ZFBMomentCell ()

@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *contentLabel;
@property (nonatomic, weak) UIImageView *pictureView;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UIButton *deleteButton;

@property (weak, nonatomic) UIView *line;

@end

@implementation ZFBMomentCell

// 如果想自定义cell子控件 需要重写initWithStyle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

// 只要设置数据,就会调用cell的set数据的方法
- (void)setMoment:(ZFBMoment *)moment {
    _moment = moment;
    // 把数据放在控件上

    self.iconView.image = [UIImage imageNamed:moment.icon];
    self.nameLabel.text = moment.name;
    self.contentLabel.text = moment.content;

    if (moment.picture) {    // 如果用户发送了图片内容

        self.pictureView.image = [UIImage imageNamed:moment.picture];

        // 在获取图片以后更新约束
        [self.pictureView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(150);
            CGFloat pictureViewWidth = 150 * [UIImage imageNamed:moment.picture].size.width / [UIImage imageNamed:moment.picture].size.height;
            make.width.offset(pictureViewWidth);

        }];
    } else {
        //  解决cell重用造成的图片显示有问题的解决方法
        [self.pictureView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.width.offset(0);
        }];
    }

    self.timeLabel.text = moment.time;

    // 判断是否应该显示删除按钮
    self.deleteButton.hidden = !moment.isMine;
    if (!moment.isMine) {
        self.deleteButton.hidden = YES;
    } else {
        self.deleteButton.hidden = NO;
    }
}

// 初始化添加cell内容的子控件
- (void)setupUI {
    CGFloat margin = 8;          // 边距
    CGFloat imageViewWH = 45;    // 头像的宽高

    // !!!所有cell的子控件 一定一定一定要添加到cell的contentView上
    // 头像
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.image = [UIImage imageNamed:@"user01_icon"];
    [self.contentView addSubview:iconView];

    // 头像布局
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(margin);
        make.width.height.offset(imageViewWH);
    }];

    // 昵称
    //    UILabel* nameLabel = [[UILabel alloc] init];
    //    nameLabel.text = @"德玛西亚";
    //    nameLabel.textColor = [UIColor colorWithHex:0x7686a8];

    UILabel *nameLabel = [UILabel labelWithText:@"" andTextColor:[UIColor colorWithHex:0x7686a8] andFontSize:14];

    [self.contentView addSubview:nameLabel];

    // 昵称布局
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconView);
        make.left.equalTo(iconView.mas_right).offset(margin);
    }];

    // 内容
    //    UILabel* contentLabel = [[UILabel alloc] init];
    //    contentLabel.text = @"呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒呵呵哒";
    UILabel *contentLabel = [UILabel labelWithText:@"" andTextColor:[UIColor darkGrayColor] andFontSize:14];
    contentLabel.numberOfLines = 0;
    [self.contentView addSubview:contentLabel];

    // 内容约束
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(margin);
        make.left.equalTo(nameLabel);
        // 如果想要让label换行 那么一定要设置label的'宽'
        make.right.offset(-margin);
    }];

    // 内容图片
    UIImageView *pictureView = [[UIImageView alloc] init];
    [self.contentView addSubview:pictureView];

    // 内容图片约束
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentLabel.mas_bottom).offset(margin);
        make.left.equalTo(contentLabel);
        make.width.height.offset(0);
    }];

    // 时间
    //    UILabel* timeLabel = [[UILabel alloc] init];
    //    timeLabel.text = @"1分钟前";
    UILabel *timeLabel = [UILabel labelWithText:@"" andTextColor:[UIColor darkGrayColor] andFontSize:12];
    [self.contentView addSubview:timeLabel];

    // 时间约束
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pictureView.mas_bottom).offset(margin);
        make.left.equalTo(pictureView);
    }];

    // 删除按钮
    UIButton *deleteButton = [[UIButton alloc] init];
    deleteButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [deleteButton setTitleColor:[UIColor colorWithHex:0x6c7ea2] forState:UIControlStateNormal];
    [deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.contentView addSubview:deleteButton];

    // 删除按钮的约束
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timeLabel.mas_right).offset(margin);
        make.centerY.equalTo(timeLabel);
    }];

    // 评论按钮
    UIButton *commentButton = [[UIButton alloc] init];
    [commentButton setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [self.contentView addSubview:commentButton];

    // 评论按钮的约束
    [commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(deleteButton);
        make.right.offset(-margin);
        // 3.设置最后一个控件和底边的约束
        make.bottom.offset(-margin);

    }];

    // 一条红色的线
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:line];

    // 红线约束
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.bottom.offset(0);
        make.height.offset(1 / [UIScreen mainScreen].scale);
    }];

    // 用全局属性记录 方法设置数据内容
    self.iconView = iconView;
    self.nameLabel = nameLabel;
    self.contentLabel = contentLabel;
    self.pictureView = pictureView;
    self.timeLabel = timeLabel;
    self.deleteButton = deleteButton;
    self.line = line;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    self.line.backgroundColor = [UIColor lightGrayColor];
}

@end
